package com.triptogether.utils.ws;

import org.json.JSONException;
import org.json.JSONObject;

import com.triptogether.utils.log.Logger;

public class WebServiceResult {

	private final String TAG = Logger.makeLogTag(this.getClass());

	public static final int HTTP_STATUS_OK = 200;
	// For set the status in the json that is going to build
	private static final String JSON_NAME_STATUS = "status";
	private static final String JSON_VALUE_STATUS_OK = "success";
	private static final String JSON_VALUE_STATUS_ERROR = "error";
	private static final String JSON_VALUE_STATUS_FAILED = "failed";
	// For the data in the json that is going to build
	private static final String JSON_NAME_DATA = "data";
	// When the status is error
	private static final String JSON_NAME_ERROR_MESSAGE = "message";
	private static final String JSON_NAME_CODE = "code";

	private JSONObject jsonResult;

	public WebServiceResult() {}

	/**
	 * Build the json result from the webService.</br>
	 * 
	 * @param responseString
	 *            The result from the webservice
	 */
	public void setOK(String responseString) {
		jsonResult = new JSONObject();
		try {
			jsonResult.put(JSON_NAME_STATUS, JSON_VALUE_STATUS_OK);
			jsonResult.put(JSON_NAME_DATA, responseString);
		} catch (JSONException e) {
			Logger.LOGE(TAG, e.getMessage(), e);
		}
	}

	/**
	 * Build a json that represents the error from the webService</br>
	 * 
	 * @param errorCode
	 *            Represents the error code from the webservice
	 * @param errorMessage
	 *            Represents the error message from the webservice
	 */
	public void setError(int errorCode, String errorMessage) {
		jsonResult = new JSONObject();
		try {
			jsonResult.put(JSON_NAME_STATUS, JSON_VALUE_STATUS_ERROR);
			jsonResult.put(JSON_NAME_CODE, errorCode);
			jsonResult.put(JSON_NAME_ERROR_MESSAGE, errorMessage);
		} catch (JSONException e) {
			Logger.LOGE(TAG, e.getMessage(), e);
		}
	}

	/**
	 * Builed a json that represents a failed of calling to webservice.</br>
	 * usually we will call that method when Exception is thrown
	 * 
	 * @param failedMessage
	 *            The message from the Exception
	 */
	public void setFailed(String failedMessage) {
		jsonResult = new JSONObject();
		try {
			jsonResult.put(JSON_NAME_STATUS, JSON_VALUE_STATUS_FAILED);
			jsonResult.put(JSON_NAME_ERROR_MESSAGE, failedMessage);
		} catch (JSONException e) {
			Logger.LOGE(TAG, e.getMessage(), e);
		}
	}

	/**
	 * 
	 * @return the message that the {@link WebServiceResult} initialized with.
	 */
	public String getErrorMessage() {
		// Get the status from the jsonResult
		String message;
		try {
			message = this.jsonResult.get(JSON_NAME_ERROR_MESSAGE).toString();
		} catch (JSONException e) {
			throw new WebServiceResultNotInitializedException();
		}
		
		return message;
	}

	public String getResponseData() {
		// Get the status from the jsonResult
		String data;
		try {
			data = this.jsonResult.get(JSON_NAME_DATA).toString();
		} catch (JSONException e) {
			throw new WebServiceResultNotInitializedException();
		}
		
		return data;
	}
	
	private String getStatus() {
		try {
			return this.jsonResult.get(JSON_NAME_STATUS).toString();
		} catch (JSONException e) {
			throw new WebServiceResultNotInitializedException();
		}
	}
	
	/**
	 * Check if the webservice succeced.
	 * 
	 * @return true if the Status is succeced, otherwise, return false
	 */
	public boolean isStatusOK() {
		return getStatus().equals(JSON_VALUE_STATUS_OK);
	}

	public boolean isStatusError() {
		return getStatus().equals(JSON_VALUE_STATUS_ERROR);
	}

	public boolean isStatusFailed() {
		return getStatus().equals(JSON_VALUE_STATUS_FAILED);
	}

	public class WebServiceResultNotInitializedException extends RuntimeException {
		private static final long serialVersionUID = -2694539915804846790L;

		public WebServiceResultNotInitializedException() {
			this("The object " + WebServiceResult.class.getName() +
			      " was not initialized with: setOK, setError or setFailed");
		}
		
		public WebServiceResultNotInitializedException(String detailMessage) {
			super(detailMessage);
		}
	}
}
